﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Transactions;

namespace WebChat.Admin
{
    public partial class CreateChatter : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void ButtonCreateChatter_OnClick(object sender, EventArgs args)
        {
            Page.Validate("ValidationGroupChatter");

            if (!Page.IsValid)
            {
                this.LiteralError.Text = "There are some invalid fields!";
                return;
            }
            
            WebChatDAL.WebChatDAO dao = new WebChatDAL.WebChatDAO();

            WebChatDAL.aspnet_Users user;
            if (dao.TryGetUserByUserName(this.TextBoxUserName.Text.Trim(), out user))
            {
                this.LiteralError.Text = "Sorry, username is taken.";
                return;
            }

            List<string> roleNames = new List<string>(){"Chatters"};

            if (this.CheckBoxAdminsRole.Checked)
            {
                roleNames.Add("Admins");
            }

            WebChatDAL.Chatter chatter = dao.CreateChatter(this.TextBoxUserName.Text.Trim(), 
                this.TextBoxPassword.Text, 
                this.TextBoxFirstName.Text.Trim(), 
                this.TextBoxLastName.Text.Trim(),
                roleNames.ToArray());

            Response.Redirect("ChatterCreateSuccess.aspx?chatterId=" + chatter.ChatterId);
        }
    }
}